<?php
class Table{
	/**
	 * @class Table
	 * @brief Util class to provide functions for creating HTML tables
	 */
	
	public static function openTable($id='', $class=''){
		echo '<table border="1" id="'.$id.'" class="'.$class.'">';
	}
	public static function closeTable(){
		echo '</table>';
	}
	public static function openRow($id='', $row_class=''){
		echo '<tr class="'.$row_class.'" id="'.$id.'">';
	}
	
	public static function closeRow(){
		echo '</tr>';
	}
	/*
		print cells | field | field | field |
	*/
	public static function printCells($fields=null, $cell_class=''){
		if($fields==null){
			return;
		}
		foreach($fields as $td){
			echo '<td>'.$td.'</td>';	
		}
	}	
	/*
		remove an element in an array
		@param $fields_to_omit
			array of search items to delete
			
		@param $field_array
			original array
			
		@param $is_omitting_key
			true if the elements in $fields_to_omit are KEYS of $fields_array
			false if the elements in $fields_to_omit are VALUES of $fields_array
			
		@return 
			the array after omitting the fields, return null if invalid arrays are provided
			
	*/
	public static function omitFields($fields_to_omit, $field_array, $is_omitting_key){
		if(!is_array($field_array) || !is_array($fields_to_omit)){
			return ;
		}
		foreach($field_array as $key=>$value){
			if($is_omitting_key){
				if(in_array($key,$fields_to_omit)){
					unset($field_array[$key]);
				}
			}else{
				if(in_array($value,$fields_to_omit)){
					unset($field_array[$key]);
				}
			}
			
		}
		
		return $field_array;
	}
	
	
	public static function printOrderableHeading($fields, $order_by='', $is_asc=true){
		if(!is_array($fields) || count($fields)==0){
			return;
		}
		if($order_by == ''){
			$order_by = $fields[0];
		}
		
		$reverse_asc = ($is_asc) ? 'DESC' : 'ASC';
		$asc = 'ASC';
		
		Table::openRow('','table-heading');
		
		$rows = array();
		foreach($fields as $db_field=>$heading){
			if($order_by == $db_field){
				$rows[] = '<a href="?orderby='.$db_field.'&asc='.$reverse_asc.'">'.$heading.'</a>';
			}else{
				$rows[] = '<a href="?orderby='.$db_field.'&asc='.$asc.'">'.$heading.'</a>';
			}
		}
		
		Table::printCells($rows);
		Table::closeRow();
	}
	
}
?>